# -*- coding: utf-8 -*-
"""
Created on Mon Aug 20 17:19:39 2012

@author: Tomek
"""


import CPPparse
import sys

class ReplaceType:
    def __init__(self,oldName,oldExtra,oldArray,newName,newExtra,newArray):
        self.OldName=oldName
        self.OldExtra=oldExtra
        self.OldArray=oldArray
        self.NewName=newName
        self.NewExtra=newExtra
        self.NewArray=newArray
    def oldEquals(self,a):
        if a.OldName==self.OldName and a.OldExtra==self.OldExtra and a.OldArray==self.OldArray:
            return True
        return False

class ConvertObject:
    def __init__(self,nm,ptr,ref,defTemplate):
        self.pointer=ptr=="True"
        self.reference=ref=="True"
        self.definedTemplates=[]
        self.name=nm
        defTable=defTemplate.split(";")
        for x in defTable:
            self.definedTemplates.append(x)
    def equals(self,a):
        if self.pointer==a.pointer and self.reference==a.reference and self.definedTemplates==a.definedTemplates and self.name==a.name:
            return True
        return False
        
class ReplaceTypeArray:
    def __init__(self):
        self.data=[]
    def exist(self,x):
        for p in self.data:
            if p.oldEquals(x):
                return True
        return False
    def append(self,x):
        if self.exist(x)!=True:
            self.data.append(x)
            
class ConvertObjectArray:
    def __init__(self):
        self.data=[]
    def exist(self,x):
        for p in self.data:
            if p.equals(x):
                return True
        return False
    def append(self,x):
        if self.exist(x)!=True:
            self.data.append(x)

replaceTypes=ReplaceTypeArray()
convertObjects=ConvertObjectArray()
globalNamespace=CPPparse._Namespace()
configFile=sys.argv[1]
outputFile=sys.argv[2]
def readParsingFile(fname):
    f=open(fname,"r")
    rstr=f.readline()
    rstr.replace("\n","")
    f.close()
    return rstr

def replaceType(t):
    nt=CPPparse._Type()
    nt.data=t.data
    for r in replaceTypes.data:
        if t["name"]==r.OldName or r.OldName=="__ANY__":
            if t["extra"]==r.OldExtra or r.OldExtra=="__ANY__":
                if t["array"]==r.OldArray or r.OldArray=="__ANY__":
                    if r.NewName!="__SAME__":
                        nt.data["name"]=r.NewName
                    if r.NewExtra!="__SAME__":
                        nt.data["extra"]=r.NewExtra
                    if r.NewArray!="__SAME__":
                        nt.data["array"]=r.NewArray
    for x in range(0,len(nt["templates"])):
        nt["templates"][x]=replaceType(nt["templates"][x])
    return nt
    

def readConfig(fname):
    f=open(fname,"r")
    readConvertType=False
    readReplaceType=False
    data=[]
    ldata=0
    for x in f.readlines():
        x=x.replace("\n","")
        if readConvertType==False and readReplaceType==False:
            if x=="REPLACE_TYPE":
                readReplaceType=True
                ldata=0
                data=[]
            elif x=="CONVERT_TYPE":
                readConvertType=True
                ldata=0
                data=[]
        elif readConvertType==True:
            if x=="__NONE__":
                x=""
            data.append(x)
            ldata=ldata+1
            if ldata>3:
                pp=ConvertObject(data[0],data[1],data[2],data[3])
                readConvertType=False
                convertObjects.append(pp)
        elif readReplaceType==True:
            if x=="__NONE__":
                x=""
            data.append(x)
            ldata=ldata+1
            if ldata>5:
                pp=ReplaceType(data[0],data[1],data[2],data[3],data[4],data[5])
                readReplaceType=False
                replaceTypes.append(pp)
    f.close()
    
readConfig(outputFile)
print readParsingFile(configFile)
globalNamespace=CPPparse.parse(readParsingFile(configFile))

for x in globalNamespace.getObjects("Type"):
    pp=ReplaceType(x["name"],x["extra"],x["array"],"__SAME__","__SAME__","__SAME__")
    
    if x["extra"]=="*":
        pp.NewExtra="__NONE__"
        pp.NewName=x["name"]+"__LUAPOINTER__"
    elif x["extra"]=="&":
        pp.NewExtra="__NONE__"
        pp.NewName=x["name"]+"__LUAREFERENCE__"
    elif x["extra"]=="":
        pp.OldExtra="__NONE__"
    if x["array"]=="":
        pp.OldArray="__NONE__"
    if x.isTemplate()==False and x["isBuiltin"]==False:
        replaceTypes.append(pp)
    
for x in globalNamespace.getAllTypes():
    replaceType(x)

for x in globalNamespace.getObjects("Typedef"):
    p=ConvertObject("","","","")
    p.name=x["type"]["name"]
    p.pointer=x["type"]["extra"]=="*"
    p.reference=x["type"]["extra"]=="&"
    p.definedTemplates=[]
    for t in x["type"]["templates"]:
        print "TEMP",t["name"]
        p.definedTemplates.append(t["name"])
    convertObjects.append(p)
    
f=open(outputFile,"w")
for x in convertObjects.data:
    f.write("CONVERT_TYPE"+"\n")
    f.write(x.name+"\n")
    if x.pointer==True:
        f.write("True\n")
    else:
        f.write("False\n")
    if x.reference==True:
        f.write("True\n")
    else:
        f.write("False\n")
    frst=0
    for p in x.definedTemplates:
        if frst==0:
            frst=1
        else:
            f.write(";")
        f.write(p)
    f.write("\n")
for x in replaceTypes.data:
    f.write("REPLACE_TYPE"+"\n")
    f.write(x.OldName+"\n")
    f.write(x.OldExtra+"\n")
    f.write(x.OldArray+"\n")
    f.write(x.NewName+"\n")
    f.write(x.NewExtra+"\n")
    f.write(x.NewArray+"\n")
f.close()